﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Common;
using CNCSoft.ErpApps.TMS.myclass;
using CNCSoft.EData;

namespace CNCSoft.ErpApps.TMS
{
    public partial class frmDangKyMonHocChoLop : Form
    {
        SqlConnection conn;
        DataSet ds;
        CommonClass mine;
        string companyID;
        private string[] selectedValue;

        public frmDangKyMonHocChoLop()
        {
            InitializeComponent();
        }

        private void frmDangKyMonHocChoLop_Load(object sender, EventArgs e)
        {
            conn = new SqlConnection(GlobalVariable.WSInfo.ConnectionString);
            ds = new DataSet();
            companyID = GlobalVariable.WSInfo.CompanyID;
            mine = new CommonClass();

            LoadControl();
            
        }

        private void searchLookUpEditLop_EditValueChanged(object sender, EventArgs e)
        {
            string makhoahoc = searchLookUpEditLop.EditValue.ToString();
            lblLop.Text = searchLookUpEditLop.Text;
            LoadGridSubject(makhoahoc);
        }

        private void simpleButtonDangKy_Click(object sender, EventArgs e)
        {
            DangKy();
            LoadGridSubject(searchLookUpEditLop.EditValue.ToString());
        }

        #region coding
        //load danh sách lớp đã ready
        private void LoadControl()
        {
            try
            {
                string sql =
                @"SELECT dmkh.MaKhoaHoc, dmkh.TenKhoaHoc
                    FROM DM_KHOAHOC dmkh
                    WHERE dmkh.MaDonVi = '" + companyID + "' AND TrangThai = 'Ready'";
                
                mine.ExecuteDataAdapter(sql, conn, ds, "TableLop", "MaKhoaHoc", "TenKhoaHoc", searchLookUpEditLop);                         
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadGridSubject(string malop)
        {
            try
            {
                DataSet dsMonHoc = new DataSet();
                string qMonHoc = "SELECT MaMonHoc,TenMonHoc FROM [dbo].[DM_MONHOC]";
                SqlDataAdapter da = new SqlDataAdapter(qMonHoc, conn);
                da.Fill(dsMonHoc, "MonHoc");
                repIMonHoc.DataSource = dsMonHoc.Tables["MonHoc"];
                repIMonHoc.DisplayMember = "TenMonHoc";
                repIMonHoc.ValueMember = "MaMonHoc";

                string q = "SELECT ID,MaMonHoc FROM [dbo].[KhoaHoc_MonHoc] WHERE MaKhoaHoc ='"+malop+"'";
                DataSet dsLopMonHoc = new DataSet();
                mine.ExecDataAdapter(q, conn, dsLopMonHoc, "TableLop-MonHoc", gridControlMonHoc);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

//        private void LoadMonHocByKhoaHoc(string makhoahoc)
//        {
//            try
//            {
//                if (signTableMonHocByKhoaHoc)
//                {
//                    ds.Tables["TableMonHocByKhoaHoc"].Rows.Clear();
//                }
//                string sql =
//                    @"SELECT DISTINCT
//                        dmmh.MaMonHoc,
//                        dmmh.TenMonHoc
//                        FROM Invoice_KhoaHoc_QTHT qtht INNER JOIN Invoice_KhoaHoc invkh
//                        ON qtht.MaHocTap = invkh.ID
//                        INNER JOIN DM_MONHOC dmmh
//                        ON dmmh.MaMonHoc = qtht.MaMonHoc
//                        WHERE invkh.MaKhoaHoc = '" + makhoahoc + "'";
//                mine.ExecuteDataAdapter(sql, conn, ds, "TableMonHocByKhoaHoc", "MaMonHoc", "TenMonHoc", cmbMonHoc);
//                signTableMonHocByKhoaHoc = true;
//            }
//            catch (Exception ex)
//            {
//                MessageBox.Show(ex.Message);
//            }
//        }

        //get column for gridview
        private object GetColumnForGridView(string sql)
        {

            object col = null;
            try
            {
                col = SQLHelper.ExecuteScalar(GlobalVariable.WSInfo.ConnectionString, sql);
                return col;
            }
            catch { return col; }

        }


        //đưa data into table KhoaHoc_MonHoc
        private void DangKy()
        {
            try
            {
                  int result = 0;
                  selectedValue = mine.ChooseAllValue(gridViewMonHoc, "MaMonHoc");
                  for (int i = 0; i < selectedValue.Length; )
                  {
                      if (this.GetColumnForGridView("SELECT ID FROM [dbo].[KhoaHoc_MonHoc] WHERE MaKhoaHoc = '"+searchLookUpEditLop.EditValue+"' AND MaMonHoc = '"+selectedValue[i]+"'") == null)
                      {
                          string sql = @"INSERT INTO [dbo].[KhoaHoc_MonHoc]([MaKhoaHoc], [MaMonHoc]) VALUES(@a, @b)";
                          SqlCommand cmd = new SqlCommand(sql, conn);
                          cmd.Parameters.AddWithValue("@a", searchLookUpEditLop.EditValue);
                          cmd.Parameters.AddWithValue("@b", selectedValue[i]);

                          conn.Open();
                          result = cmd.ExecuteNonQuery();
                          conn.Close();
                      }

                      i++;
                  }         

               
                if (result > 0)
                {
                    MessageBox.Show("Đăng ký môn học cho lớp thành công");
                }
                else
                {
                    MessageBox.Show("Đăng ký môn học cho lớp thất bại");
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (conn != null)
                    conn.Close();
            }
        }
        #endregion

        private void DeleteMonHoc(int id)
        {
            try
            {
                int i = 0;
                string sql = @"DELETE FROM [dbo].[KhoaHoc_MonHoc] WHERE ID = @id";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@id", id);
                try
                {
                    conn.Open();
                    i = cmd.ExecuteNonQuery();
                }
                finally
                {
                    if (conn != null)
                        conn.Close();
                }
                if (i > 0)
                {
                    MessageBox.Show("Hủy thành công");
                }
                else
                    MessageBox.Show("Hủy thất bại");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnHuyMonHoc_Click(object sender, EventArgs e)
        {
            try
            {
                string[] choosedValues = new string[2];
                choosedValues = mine.ChoosedValue(gridViewMonHoc, null);
                if (!string.IsNullOrEmpty(choosedValues[0].ToString()))
                {

                    string id = choosedValues[0].ToString();
                    DeleteMonHoc(Convert.ToInt32(id));
                    LoadGridSubject(searchLookUpEditLop.EditValue.ToString());
                }
                else
                {
                    MessageBox.Show("Mời chọn môn học muốn hủy");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }//the end class
}//the end namespace
